package com.circle.interests.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.circle.common.core.exception.RRException;
import com.circle.common.core.utils.R;
import com.circle.common.core.validator.AddGroup;
import com.circle.common.core.validator.UpdateGroup;
import com.circle.interests.biz.entity.PrizeInfo;
import com.circle.interests.biz.request.PrizeInfoModifyInventoryRequest;
import com.circle.interests.biz.request.PrizeInfoPageRequest;
import com.circle.interests.biz.request.PrizeInfoRequest;
import com.circle.interests.biz.service.IPrizeInfoService;
import com.circle.prize.library.api.enums.InventoryTypeEnum;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhou
 * @date 2021/12/5
 */
@Slf4j
@Api(tags = {"奖品信息管理（pc端接口）"})
@RestController
@RequestMapping("prizeInfo")
@ApiSupport(author = "周福寿")
public class PrizeInfoController {

    @Resource
    private IPrizeInfoService PrizeInfoService;

    @ApiOperation("新增")
    @PostMapping("/add")
    public R<?> add(@Validated({AddGroup.class}) @RequestBody PrizeInfoRequest params) {
        if (InventoryTypeEnum.UNLIMITED.getCode().equals(params.getInventoryType())) {
            // 不限量库存值设为空
            params.setTotal(null);
        } else {
            if (params.getTotal() <= 0) {
                throw new RRException("库存总数只能大于0");
            }
        }
        PrizeInfoService.add(params);
        return R.success();
    }

    @ApiOperation("详情")
    @PostMapping("/info/{id}")
    public R<PrizeInfo> info(@ApiParam("活动分类ID") @PathVariable Long id) {
        return R.success(PrizeInfoService.getById(id));
    }

    @ApiOperation("编辑")
    @PostMapping("/update")
    public R<?> update(@Validated({UpdateGroup.class}) @RequestBody PrizeInfoRequest params) {
        PrizeInfoService.updateById(params);
        return R.success();
    }

    @ApiOperation("删除")
    @PostMapping("/del/{id}")
    public R<?> del(@ApiParam("活动分类ID") @PathVariable Long id) {
        PrizeInfoService.deleteById(id);
        return R.success();
    }

    @ApiOperation("列表")
    @PostMapping("/list")
    public R<List<PrizeInfo>> list(@ApiParam(value = "奖品分类id") @RequestParam(value = "categoryId", required = false) Long categoryId) {
        return R.success(PrizeInfoService.list(categoryId));
    }

    @ApiOperation("分页")
    @PostMapping("/page")
    public R<IPage<PrizeInfo>> page(@Validated @RequestBody PrizeInfoPageRequest params) {
        return R.success(PrizeInfoService.page(params));
    }

    @ApiOperation("增加库存")
    @PostMapping("/addInventory")
    public R<Boolean> addInventory(@Validated @RequestBody PrizeInfoModifyInventoryRequest params) {
        return R.success(PrizeInfoService.modifyInventory(params.getPrizeId(), params.getCount()));
    }
}
